# -*- coding: utf-8 -*-
import hashlib
from actions.weixin import *
from operations.weixin.weixin_operation import WeixinOperation

wxOperation = WeixinOperation()
weixinAction = Blueprint("weixin_action",__name__)

@weixinAction.route("/",methods=[HTTP_GET])
def weixin_get():
    signature = request.args.get("signature")
    timestamp = request.args.get("timestamp")
    nonce = request.args.get("nonce")
    echostr = request.args.get("echostr")
    token = "etlinking"
    #token = "zjkpython"
    tmpList = [token,timestamp,nonce]
    tmpList.sort()
    tmpStr = "".join(tmpList)
    hashStr = hashlib.sha1(tmpStr).hexdigest()
    if hashStr == signature:
        return echostr #success
    return "access verification fail" #fail

@weixinAction.route("/",methods=[HTTP_POST])
def weixin_post():
    xmlData = request.data
    result = json.loads(wxOperation.saveData(xmlData))['Data']
    if(result is not None):
        return result
    return ""

@weixinAction.route("/authorize",methods=[HTTP_GET,HTTP_POST])
def authorize():
    return 'hello'

@weixinAction.route("/dev_menu",methods=[HTTP_GET])
def createDevMenu():
    menus = '''
     {
    "button": [
        {
            "name": "交易", 
            "sub_button": [
                {
                    "type": "click", 
                    "name": "我的状态", 
                    "key": "VZT_TEXT"
                },
                {
                    "type": "click", 
                    "name": "购买奴隶", 
                    "key": "VGM_TEXT"
                }, 
                {
                    "type": "click", 
                    "name": "我的奴隶", 
                    "key": "VNL_TEXT"
                }, 
                {
                    "type": "click", 
                    "name": "我要赎身", 
                    "key": "VSS_TEXT"
                }
            ]
        }, 
        {
            "name": "我的手段", 
            "sub_button": [
                {
                    "type": "click", 
                    "name": "折磨奴隶", 
                    "key": "VZM_TEXT"
                }, 
                {
                    "type": "click", 
                    "name": "讨好主人", 
                    "key": "VTH_TEXT"
                }, 
                {
                    "type": "click", 
                    "name": "求包养", 
                    "key": "VBY_TEXT"
                }
            ]
        }, 
        {
            "name": "我的ET", 
            "sub_button": [ 
                {
                    "type": "click", 
                    "name": "每日签到", 
                    "key": "VQD_TEXT"
                },
                {
                    "type": "view", 
                    "name": "购买", 
                    "url": "http://etspace.taobao.com/"
                }, 
                {
                    "type": "view", 
                    "name": "关于我们", 
                    "url": "http://www.etlinking.com/"
                }, 
                {
                    "type": "click", 
                    "name": "绑定ET", 
                    "key": "VBD_TEXT"
                }
            ]
        }
    ]
}
    '''
    result = wxOperation.createMenu(menus)#json.loads(wxOperation.createMenu(menus))
    return result

@weixinAction.route("/edit_menu",methods=[HTTP_GET])
def createEditMenu():
    menus = '''

    '''
    result = json.loads(wxOperation.createMenu(menus))
    return result



@weixinAction.route("/index",methods=[HTTP_GET,HTTP_POST])
def weixin_index():
    return str(request.__dict__)#render_template("/weixin/index.html",requestData = {"id":2,"name":"test"})

@weixinAction.route("/xml",methods=[HTTP_GET])
def weixin_xml():
    xmlStr = """<xml>
                    <ToUserName><![CDATA[toUser]]></ToUserName>
                    <FromUserName><![CDATA[fromUser]]></FromUserName>
                    <CreateTime>123456789</CreateTime>
                    <MsgType><![CDATA[event]]></MsgType>
                    <Event><![CDATA[LOCATION]]></Event>
                    <Latitude>23.137466</Latitude>
                    <Longitude>113.352425</Longitude>
                    <Precision>119.385040</Precision>
                </xml>"""

    xmlData = """<xml>
                    <ToUserName><![CDATA[toUser]]></ToUserName>
                    <FromUserName><![CDATA[fromUser]]></FromUserName>
                    <CreateTime>1351776360</CreateTime>
                    <MsgType><![CDATA[location]]></MsgType>
                    <Location_X>23.134521</Location_X>
                    <Location_Y>113.358803</Location_Y>
                    <Scale>20</Scale>
                    <Label><![CDATA[位置信息]]></Label>
                    <MsgId>1234567890123456</MsgId>
                </xml>"""
    #xmlRoot = ElementTree.fromstring(xmlStr1)
    #eventNode = xmlRoot.find("Event")
    #return "%s %s" % (eventNode.tag,eventNode.text)
    #return xmlStr
    result = wxOperation.saveData(xmlData)
    return result

@weixinAction.route("/update_follower",methods=[HTTP_GET])
def weixin_xml():
    
    #xmlRoot = ElementTree.fromstring(xmlStr1)
    #eventNode = xmlRoot.find("Event")
    #return "%s %s" % (eventNode.tag,eventNode.text)
    #return xmlStr
    wxOperation.batchUpdateUser()
    return 'hello world!'

@weixinAction.route("/get_all_follower",methods=[HTTP_GET])
def weixin_xml():
    
    #xmlRoot = ElementTree.fromstring(xmlStr1)
    #eventNode = xmlRoot.find("Event")
    #return "%s %s" % (eventNode.tag,eventNode.text)
    #return xmlStr
    #wxOperation.batchInsertUser()
    return 'hello world!'



